import { defineStore } from 'pinia'
import {
  deleteClubById,
  getAllClubs,
  getClubById,
  getClubByName,
  getClubMemberByClubId,
  postCreateClub,
  reviewMemberById,
  updateClub,
  deleteMemberById,
  updateClubStatus,
  getClubByUserId,
	getJoinedClubByUserId,
	getClubMemberStatus,
	joinClub,
	quitClub
} from '@/service/club.js'
import { LIMIT } from '@/global/contants.js'
const useClubStore = defineStore('club', {
  state: () => ({
    clubsList: [],
    clubsTotalCount: 0,
    clubDetail: {},
    clubMemebrs: [],
	joinedClubs: [],
	clubMemberInfo: '',
	pendingClubMemberCount: 0,
    clubMemebersTotalCount: 0
  }),
  actions: {
    // 获取全部社团信息
    async getClubsListAction(limit = LIMIT, offset = 0, status = 'approved') {
      const clubsListResult = await getAllClubs(limit, offset, status)
      const { totalCount, list } = clubsListResult.data
      this.clubsList = list
      this.clubsTotalCount = totalCount
    },

    // 根据社团名称获取社团
    async getClubListByNameAction(
      name,
      limit = 10,
      offset = 0,
      status = 'approved'
    ) {
      const clubsListResult = await getClubByName(name, limit, offset, status)
      const { totalCount, list } = clubsListResult.data
	  console.log(list);
      this.clubsList = list
      this.clubsTotalCount = totalCount
    },

    // 创建社团
    async newClubDataAction(clubInfo) {
      const newResult = await postCreateClub(clubInfo)
      this.getClubsListAction()
    },

    // 删除社团
    async deleteClubByIdAction(id) {
      const deleteResult = await deleteClubById(id)
      this.getClubsListAction()
	  this.getClubByUserIdAction()
      return deleteResult
    },

    // 更新社团
    async editClubDataAtion(id, clubInfo) {
      const editResult = await updateClub(id, clubInfo)
      this.getClubsListAction()
    },

    // 更新社团状态
    async updateClubStatusAction(id, status) {
      const result = await updateClubStatus(id, status)
    },

    /** detail部分 */
    // 根据id获取社团详情
    async getClubDetaiByIdAction(id) {
      const detailResult = await getClubById(id)
      this.clubDetail = detailResult.data
    },
    // 获取社团成员的详情
    async getClubMemberByClubIdAction({
		id,
      limit,
      offset,
      name,
      status}) {
      const clubMembersResult = await getClubMemberByClubId(
        id,
        limit,
        offset,
        name,
        status
      )
      const { totalCount, list } = clubMembersResult.data
      this.clubMemebrs = list.filter(
        (item) => item.user_id !== this.clubDetail.leader_id
      )
      this.clubMemebersTotalCount = totalCount
    },
	
	async getPendingMemberCountAction(club_id, limit = LIMIT, offset, name) {
		const clubMembersResult = await getClubMemberByClubId(club_id, limit, offset, name, 'pending')
		const { totalCount, list } = clubMembersResult.data
		const clubMemebrs = list.filter(
        (item) => item.user_id !== this.clubDetail.leader_id
		)
		this.pendingClubMemberCount = clubMemebrs.length
	},
	
    // 更改成员状态
    async setClubMemberStatusAction(
      club_id,
      id,
      status
    ) {
      await reviewMemberById(club_id, id, status)
      this.getClubMemberByClubIdAction({id:club_id,limit: LIMIT,status:'pending'})
    },
    // 删除成员
    async deleteClubMemebrAction(club_id, user_id) {
      await deleteMemberById(club_id, user_id)
      this.getClubsListAction()
	  this.getClubMemberByClubIdAction({id:club_id,limit: LIMIT,status:'rejected'})
    },
    // 根据用户id获取社团
    async getClubByUserIdAction() {
      const res = await getClubByUserId()
      this.joinedClubs = res.data
    },
	// 根据id查询参与了社团的人
	async getJoinedClubByUserIdAction(user_id) {
		const res = await getJoinedClubByUserId(user_id)
		this.joinedClubs = res.data
	},
	
	// 获取个人的社团状态
	async getClubMemberStatusAction(club_id) {
		const res = await getClubMemberStatus(club_id)
		this.clubMemberInfo = res.data[0]
	},
	// 参加社团
	async joinClubAction(club_id) {
		console.log(club_id)
		const res = await joinClub(club_id)
		this.getClubMemberStatusAction(club_id)
	},
	// 退出社团
	async quitClubAction(club_id) {
		const res = await quitClub(club_id)
		this.getClubMemberStatusAction(club_id)
		this.getClubMemberByClubIdAction(club_id, LIMIT)
	}
  }
})

export default useClubStore
